package com.teame.readbaby;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.teame.readbaby.parser.OrderCancelParser;
import com.teame.readbaby.parser.OrderListParser;
import com.teame.readbaby.util.ConstantValue;
import com.teame.readbaby.util.GloableParams;
import com.teame.readbaby.vo.OrderDetail;
/**
 * 我的订单
 * @author dupengtao
 *
 */
public class MyOrderActivity extends BaseActivity {
	
	private TextView tv_order_Null;
	private TextView back;
	private TextView tv_order_Mon;
	private TextView tv_order_All;
	private TextView tv_order_Not;
	private ListView lv_order;
	
	private String[] orderids;
	
	private String type = "1";//type类型默认为1 ==> 近一个月的订单
	private int selectedColor;
	private int normalColor;
	private String url;
	
	private List<Map<String, String>> data;
	private String orderId;
	private int position;
	private Map<String, String> params = new HashMap<String, String>();
	private TextView cancelOrder;
	
	final static int NOTCANCELORDER = 0;
	final static int SUCCESS = 1;
	
	private boolean stats = false;
	
	Handler handler = new Handler(){

		@Override
		public void handleMessage(Message msg) {
			switch(msg.what){
			case 0:
				Toast.makeText(context, "订单不可以修改", 0).show();
				break;
			case 1:
				data.remove(position);
				lv_order.invalidate();
				Toast.makeText(context, "订单已取消", 0).show();
				break;
			}
		}
		
	};
	
	@Override
	public void onCreate(Bundle paramBundle) {
		super.onCreate(paramBundle);
	}

	public void onClick1(View v) {
		int event = v.getId();
		switch(event){
		case R.id.head_back_text:
			//TODO Intent开启上一层界面
			this.finish();
			break;
		case R.id.my_order_month:
			if(type == "1"){
				return ;
			}else{
				type = "1";
				changeView(type);
			}
			break;
		case R.id.my_order_all:
			if(type == "2"){
				return ;
			}else{
				type = "2";
				changeView(type);
			}
			break;
		case R.id.my_order_notsend:
			if(type == "3"){
				return ;
			}else{
				type = "3";
				changeView(type);
			}
			break;
/*		case R.id.textCancelOrder:
			url = ConstantValue.HOST+ConstantValue.ORDERCANCEL;
			params.put("orderId", orderid);
			processLogic();
			break;*/
		}
	}
	
	private void changeView(String type){
//		params.put("type", type);
		replaceView(type);
		processLogic();
	}

	private void replaceView(String type) {
		if("1".equals(type)){
			tv_order_Mon.setBackgroundResource(R.drawable.segment_selected_1_bg);
			tv_order_All.setBackgroundResource(R.drawable.segment_normal_2_bg);
			tv_order_Not.setBackgroundResource(R.drawable.segment_normal_3_bg);
			
			tv_order_Mon.setTextColor(selectedColor);
			tv_order_All.setTextColor(normalColor);
			tv_order_Not.setTextColor(normalColor);
		}else if("2".equals(type)){
			tv_order_Mon.setBackgroundResource(R.drawable.segment_normal_1_bg);
			tv_order_All.setBackgroundResource(R.drawable.segment_selected_2_bg);
			tv_order_Not.setBackgroundResource(R.drawable.segment_normal_3_bg);
			
			tv_order_Mon.setTextColor(normalColor);
			tv_order_All.setTextColor(selectedColor);
			tv_order_Not.setTextColor(normalColor);
		}else{
			tv_order_Mon.setBackgroundResource(R.drawable.segment_normal_1_bg);
			tv_order_All.setBackgroundResource(R.drawable.segment_normal_2_bg);
			tv_order_Not.setBackgroundResource(R.drawable.segment_selected_3_bg);
			
			tv_order_Mon.setTextColor(normalColor);
			tv_order_All.setTextColor(normalColor);
			tv_order_Not.setTextColor(selectedColor);
		}
	}

	@Override
	protected void findViewById() {
		tv_order_Null = (TextView) findViewById(R.id.my_order_null_text);
		
		back = (TextView) findViewById(R.id.head_back_text);
		tv_order_Mon = (TextView) findViewById(R.id.my_order_month);
		tv_order_All = (TextView) findViewById(R.id.my_order_all);
		tv_order_Not = (TextView) findViewById(R.id.my_order_notsend);
		
		cancelOrder = (TextView) findViewById(R.id.textCancelOrder);
		
		lv_order = (ListView) findViewById(R.id.my_order_list);
		
		selectedColor = getResources().getColor(R.color.white);
		normalColor = getResources().getColor(R.color.normal);
		
//		url = ConstantValue.HOST+ConstantValue.ORDERLIST;
	}

	@Override
	protected void loadViewLayout() {
		setContentView(R.layout.my_order_activity);
	}

	@Override
	protected void processLogic() {
		if(stats){
			parserOrderCancel();
			stats = false;
		}else
			parserOrderList();
	}

	private void parserOrderCancel() {
		url = ConstantValue.HOST+ConstantValue.ORDERCANCEL;
		params.put("orderId", orderId);
		getDataFromServer(url, params, new OrderCancelParser(), new DataCallback<OrderDetail>() {

			@Override
			public void processData(OrderDetail paramObject, boolean paramBoolean) {
				if(!paramBoolean || paramObject == null){
					
				}else{
					data.get(position).put("cancel", paramObject.isOrdercancel()+"");
				}
			}
		});
		
	}

	private void parserOrderList() {
		url = ConstantValue.HOST+ConstantValue.ORDERLIST;
		params.put("type", type);
		this.getDataFromServer(url, params, new OrderListParser(), new DataCallback<List<OrderDetail>>() {

			@Override
			public void processData(List<OrderDetail> paramObject,
					boolean paramBoolean) {
				if(!paramBoolean || paramObject == null){
					showNoOrder();
				}else{
					
					data = new ArrayList<Map<String, String>>();
					orderids = new String[paramObject.size()];
					for (int i = 0; i < paramObject.size(); i++) {
						OrderDetail orderDetail = paramObject.get(i);
						
						Map<String, String> map = new HashMap<String, String>();
						map.put("orderid", orderDetail.getOrderid());
						orderids[i] = orderDetail.getOrderid();
						map.put("price", "￥："+orderDetail.getPrice());
						map.put("time", orderDetail.getTime());
						map.put("status", orderDetail.getStatus());
						map.put("flag", orderDetail.getFlag()+"");
						map.put("orderCancel", orderDetail.isOrdercancel()+"");
						String XXX = "";
						switch(orderDetail.getFlag()){
						//TODO 服务器根本没有返回支付方式的数据, flag文档上不是说是订单标识吗? ==> 是否可修改订单啊
						//支付类型，1=>货到付款 2=>货到POS机 3=>支付宝(待定)
						case 1:
							XXX = "可修改可删除";
							break;
						case 2:
							XXX = "不可修改";
							break;
						case 3:
							XXX = "已完成";
							break;
						}
						map.put("payment", XXX);
						data.add(map);
					}
//					String[] from = new String[]{"orderid", "price", "time", "status", "flag"};
//					int[] to = new int[]{R.id.orderId_text, R.id.orderPrice_text,
//								R.id.orderTime_text, R.id.orderState_text, R.id.orderPayment_text};
//					lv_order.setAdapter(new SimpleAdapter(MyOrderActivity.this, data, R.layout.my_order_listitem, from, to));
					lv_order.setAdapter(new MyAdapter());
				}
			}

		});
	}
	
	class MyAdapter extends BaseAdapter{

		@Override
		public int getCount() {
			return data.size();
		}

		@Override
		public Object getItem(int position) {
			orderId = data.get(position).get("orderId");//这段代码先放在这儿
			return null;
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(final int position, View convertView, ViewGroup parent) {
			ViewHolder holder = null;
			if (convertView == null) {
				convertView = View.inflate(context, R.layout.my_order_listitem, null);
				System.out.println("asgdaf");
				holder = new ViewHolder();
				holder.order_item_rel = (RelativeLayout) convertView.findViewById(R.id.order_item_rel);
				holder.orderId = (TextView) convertView.findViewById(R.id.orderId_text);
				holder.orderPrice = (TextView) convertView.findViewById(R.id.orderPrice_text);
				holder.orderTime = (TextView) convertView.findViewById(R.id.orderTime_text);
				holder.orderStatus = (TextView) convertView.findViewById(R.id.orderState_text);
				holder.orderPayment = (TextView) convertView.findViewById(R.id.orderPayment_text);
				
				holder.selected = (TextView) convertView.findViewById(R.id.textCancelOrder);//取消

				// A tag can be used to mark a view in its hierarchy and does not have to be unique within the hierarchy.
				// Tags can also be used to store data within a view without resorting to another data structure.

				convertView.setTag(holder);

			} else {
				holder = (ViewHolder) convertView.getTag();
			}

			final Map<String, String> map = data.get(position);
			if(!("可修改可删除".equals(map.get("flag")))){
				holder.selected.setEnabled(false);
				holder.selected.setBackgroundResource(R.drawable.button_long_normal);
			}

			orderids[position] = map.get("orderId");//不知道有没有用
			holder.orderId.setText(map.get("orderid"));
			holder.orderPrice.setText(map.get("price"));
			holder.orderTime.setText(map.get("time"));
			holder.orderStatus.setText(map.get("status"));
			holder.orderPayment.setText(map.get("payment"));
			
			holder.order_item_rel.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					Intent intent = new Intent(context, MyOrderDetailActivity.class);
//					intent.putExtra("orderid", data.get(position).get("orderId"));
					intent.putExtra("orderid", map.get("orderId"));
					startActivity(intent);
				}
			});

			holder.selected.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					if("1".equals(map.get("flag"))){
						stats = true;
						processLogic();
					}
					Message msg = new Message();
					if("true".equals(map.get("cancel"))){
//						data.remove(position);
//						notifyDataSetChanged();	
						msg.what = SUCCESS;
					}else{
						msg.what = NOTCANCELORDER;
					}	
					handler.sendMessage(msg);
				}
			});

			return convertView;
		}
		
		private class ViewHolder {
			RelativeLayout order_item_rel;
			TextView orderId;
			TextView orderPrice;
			TextView orderTime;
			TextView orderStatus;
			TextView orderPayment;
			TextView selected;

		}
		
	}
	
	private void showNoOrder() {
		tv_order_Null.setVisibility(View.VISIBLE);
	}

	@Override
	protected void setListener() {
/*		lv_order.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
//				MyOrderActivity.this.position = position;
				orderid = orderids[position];
				Intent intent = new Intent(context, MyOrderDetailActivity.class);
				intent.putExtra("orderid", orderid);
				startActivity(intent);
			}
			
		});*/
		
		back.setOnClickListener(this);
		
		tv_order_Mon.setOnClickListener(this);
		tv_order_All.setOnClickListener(this);
		tv_order_Not.setOnClickListener(this);
		
//		cancelOrder.setOnClickListener(this);
	}

	@Override
	protected void onClickEvent(View paramView) {
		onClick1(paramView);
	}
	
}
